package Utilities;

import java.util.ArrayList;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class MyTable extends DefaultTableModel {
	private static final long serialVersionUID = 1L;

	public static int IMG_X = 100;
	public static int IMG_Y = 70;

	private JFrame frame;
	DefaultTableModel model = new DefaultTableModel(){
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public java.lang.Class<?> getColumnClass(int arg0) {
			if(arg0 == 5){
				return Icon.class;
			}
			return String.class;
		};
	};
	JTable table = new JTable(model)  {
		private static final long serialVersionUID = 1L;

		public boolean isCellEditable(int row, int column) {
			return column == 0;
		}
		
	};

	public MyTable(JFrame frame) {
		this.frame = frame;
		constructTable();
	}

	private void constructTable() {
		String[] columnNames = {"User", "Estabelecimento", "Prato",
				"Avaliacao", "Comentarios", "Fotografia" };
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 100, 765, 300);
		for (int i = 0; i < columnNames.length; i++) {
			model.addColumn(columnNames[i]);
		}

		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setCellSelectionEnabled(true);
		table.setColumnSelectionAllowed(true);

		table.getColumnModel().getColumn(0).setPreferredWidth(50);
		table.getColumnModel().getColumn(2).setPreferredWidth(110);

		table.getColumnModel().getColumn(3).setPreferredWidth(33);
		table.getColumnModel().getColumn(5).setPreferredWidth(180);
		table.setBounds(10, 100, 765, 300);
		table.setRowHeight(IMG_Y+10);
		
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		table.setDefaultRenderer(String.class, centerRenderer);

		frame.getContentPane().add(scrollPane);
		scrollPane.setViewportView(table);

	}

	public void setData(ArrayList<Object[]> datalist) {

		while (model.getRowCount() > 0) {
			for (int i = 0; i < model.getRowCount(); i++) {
				model.removeRow(i);
			}
		}
		for (Object[] object : datalist) {
			model.addRow(object);
		}
	}
	public JTable getTable() {
		return table;
	}
	public DefaultTableModel getModel() {
		return model;
	}
}